package com.tuyan.aixiang.activity.config;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

import com.ezviz.opensdk.wificonfig.APWifiConfig;
import com.tuyan.aixiang.MainApplication;
import com.tuyan.aixiang.common.BasePresenter;
import com.tuyan.aixiang.model.YsDevice;
import com.tuyan.aixiang.util.Log;
import com.tuyan.aixiang.util.ToastUtil;
import com.tuyan.aixiang.util.WifiUtil;
import com.videogo.openapi.EZWifiConfigManager;

public class WifiConfigPresenter extends BasePresenter<WifiConfig.View> implements WifiConfig.Presenter{

    private static final String TAG = "WifiConfigPresenter";

    private YsDevice device;

    @Override
    public void init(YsDevice device) {
        this.device = device;
//        String ssid = "HAP_" + device.getDeviceSerial();
        String ssid = "EZVIZ_" + device.getDeviceSerial();
        String password = "EZVIZ_" + device.getVerifyCode();
//        String password = "AP" + device.getVerifyCode();
        this.device.setSsid(ssid);
        this.device.setPassword(password);
        iView.get().showDeviceInfo(this.device);
    }

    @Override
    public void copyPass() {
        ClipboardManager clipboardManager = (ClipboardManager) MainApplication.getAppContext().getSystemService(Context.CLIPBOARD_SERVICE);
        String pass = "EZVIZ_" + device.getVerifyCode();
        ClipData data = ClipData.newPlainText("appass", pass);
        clipboardManager.setPrimaryClip(data);
        ToastUtil.makeShort("密码已复制");
    }


    @Override
    public void deviceSearch() {
        String ssidStr = "\"" + this.device.getSsid() + "\"";
        if (WifiUtil.getInstance().getCurrentWifi().getSSID().equals(ssidStr)) {
            ToastUtil.makeShort("设备已连接,请进行下一步操作");

        } else {
            ToastUtil.makeShort("请连接至设备热点后操作");
        }

//        EZHCNetDeviceSDK.getInstance().startLocalSearch(ezsadpDeviceInfo -> {
//            Log.d(TAG, "on deviceFound" + ezsadpDeviceInfo.getDeviceSerial() + " " + ezsadpDeviceInfo.getDeviceMac());
//            EZHCNetDeviceSDK.getInstance().stopLocalSearch();
//            boolean isActivited = ezsadpDeviceInfo.isActived();
//            if(!isActivited) {
////sadp 局域网设备激活
//                new Thread(() -> {
//                    final int result = EZHCNetDeviceSDK.getInstance().activeDeviceWithSerial(device.getDeviceSerial(), device.getVerifyCode());
//                    if (result == 1) {
//                        Log.d(TAG, "Activation successful");
//                        // TODO: 2017/8/15 Activation successful
//                        return;
//                    }else {
//                        //TODO: 2017/8/15 Activation failed
////                                runOnUiThread(new Runnable() {
////                                    @Override
////                                    public void run() {
////                                        if(result == 2020){
////                                            // TODO: 2017/8/16 密码太弱
////
////                                        }else{
////                                            // TODO: 2017/8/16 激活失败
////
////                                        }
////                                    }
////                                });
//                    }
//                }).start();
//            }
//        });
    }

    @Override
    public void deviceConnect(String WifiSsid, String WifiPassword) {

        Log.d(TAG, "deviceConnect " + WifiSsid);
        EZWifiConfigManager.startAPConfigWifiWithSsid(
                WifiSsid,
                WifiPassword,
                this.device.getDeviceSerial(),
                this.device.getVerifyCode(),
                this.device.getSsid(),
                this.device.getPassword(),
                true,
                new APWifiConfig.APConfigCallback() {
                    @Override
                    public void onSuccess() {
                        EZWifiConfigManager.stopAPConfigWifiWithSsid();
                        iView.get().configSuccess();
                    }

                    @Override
                    public void OnError(int code) {
                        EZWifiConfigManager.stopAPConfigWifiWithSsid();
                        iView.get().configFailed(code);
                    }
                }
        );

    }
}

